package functions

import (
	"encoding/json"

	"github.com/project-flogo/core/data"
	"github.com/project-flogo/core/data/expression/function"
)

func init() {
	err := function.Register(&fnToJSON{})
	if err != nil {
		panic(err)
	}
}

type fnToJSON struct{}

func (fnToJSON) Name() string {
	return "marshalJSON"
}

func (fnToJSON) Sig() (paramsTypes []data.Type, isVariadic bool) {
	return []data.Type{data.TypeAny}, false
}

func (fnToJSON) Eval(params ...interface{}) (interface{}, error) {
	data := params[0]
	b, err := json.Marshal(data)
	return b, err
}
